package com.zwjstudy.cachestudy.springcache.redis.service.impl;


import com.zwjstudy.cachestudy.springcache.redis.entity.Company;
import com.zwjstudy.cachestudy.springcache.redis.service.CompanyService;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Random;

/**
 * ClassName: CompanyServiceImpl
 * Package: com.zwjstudy.cachestudy.springcache.caffeine.service.impl
 * Description:
 *
 * @Author: 张文俊
 * @Create: 2025-09-20 - 11:36
 * @Version: v1.0
 */
@Service
@CacheConfig(cacheNames = "companyCache",cacheManager = "companyCacheManager")
public class CompanyServiceImpl implements CompanyService {
    @Override
    @Cacheable(key="#id",unless = "#result == null")
    public Company getById(Long id) {

        //解决缓存穿透
        if(id==null || id<0 || id>10000){
            return null;
        }

        int i = new Random().nextInt(60);

        Company company=new Company(id,"大连广场"+i);
        System.out.println("加载最新的企业数据到缓存中"+company);


        return company;
    }
}
